<?php

namespace app\admin\controller;

use think\facade\Request;
use app\attachment\controller\Admin as AdminImage;
use app\admin\model\System as SystemModel;
class System extends Base
{
    protected $model;

    protected function initialize()
    {
        parent::initialize();
        $this->model = SystemModel::getInstance();
    }

    /*
     * base64图片上传
     *
     * */
    public function base64Image()
    {
        $config_array = $this->model->getOne(['admin_id'=>$this->admin_id],['field'=>['oss_type','oss_access_key','oss_secret_key','oss_bucket','oss_endpoint','oss_domain','qiniu_access_key','qiniu_secret_key','qiniu_bucket','qiniu_domain']]);
        //文件内容
        $config_array['imgString'] = $this->request->param("imgString") ?? "";
        //实例化图片上传类
        $attr_model = new AdminImage();
        $rs = $attr_model->baseImage($this->admin_id,$config_array);
        return array2json($rs);
    }

    // 网站基本设置
    public function index()
    {
        return $this->fetch();
    }

    /*
     * 站点设置页面
     * */
    public function setsite()
    {
        $config_array = $this->getConfig($this->admin_info);
        $this->assign("config_array",$config_array);
        return $this->fetch();
    }
    /*
     * 站点设置页面
     * */
    public function setwechat()
    {
        $config_array = $this->getConfig($this->admin_info);
        $this->assign("config_array",$config_array);
        return $this->fetch();
    }
    /*
     * 站点设置页面
     * */
    public function setoss()
    {
        $config_array = $this->getConfig($this->admin_info);
        $this->assign("config_array",$config_array);
        return $this->fetch();
    }

    /*
     * 修改后台配置
     * */
    public function setConfig()
    {
        if ($this->request->isPost() && isset($this->admin_id)) {
            /*
             * 这里需要判断是否为总账号，如果不是 需要去校验该管理员是否有配置后台信息的权限
             * */
            //用户不允许修改配置信息
            if ($this->admin_level == 3) {
                return false;
            }
            //管理员需校验权限
            if ($this->admin_level != 1) {
                //获取当前方法路径
                $path = Request::module() . "/" . Request::controller() ."/". Request::action();
            }

            //获取参数
            $data = $this->request->param();
            //删除值为空的元素
            $data = array_filter($data,function ($node){
                if($node == ''){
                    return false;
                }
                return true;
            });

            //查询该管理员配置信息是否存在 不存在新增一条记录，存在则修改
            $res = $this->model->getOne(['admin_id' => $this->admin_id]);
            if (empty($res)) {
                //新增需要写入管理员ID及创建时间
                $data['admin_id'] = $this->admin_id;
                $data['update_time'] = $data['create_time'] = time();
                $rs = $this->model->addData($data);
                return $rs;
            } else {
                $data['update_time'] = time();
                $rs = $this->model->where(['admin_id' => $this->admin_id])->update($data);
                return $rs;
            }
        }
        return 0;
    }


    /*
     * 获取后台配置
     * @param  admin_id  管理员ID
     * @param  level  管理级别
     * @return 该账户的配置信息
     * */
    public function getConfig($admin_info)
    {
        switch ($admin_info['level']) {
            //超管
            case 1:
                $rs = $this->model->getOne(['admin_id' => $admin_info['id']]);
                break;
            //管理员
            case 2:
                $rs = $this->model->getOne(['admin_id' => $admin_info['id']]);
                if (!$rs) {
                    $rs = $this->model->getOne(['id' => $admin_info['parent_id']]);
                }
                break;
            //用户
            default:
                //查询当前父级管理员的配置信息
                $rs = $this->model->getOne(['admin_id' => $admin_info['parent_id']]);
                if(!$rs){
                    $rs = $this->model->getOne(['founder' => 1]);
                }
                break;
        }
        return $rs;
    }

    /*
     * 创建微信验证文件
     * */
    public function createTxt() {
        $file_name = $this->request->param("file_name") ?? "";
        if (empty($file_name)) {
            return false;
        }
        $content = preg_replace("/MP_verify_|\.txt/","",$file_name);
        return file_put_contents($file_name,$content);
    }

    /*
     * 测试云存储
     * */
    public function testUp(){
        $config_array = $this->request->param() ?? "";
        $config_array['imgString'] = file_get_contents(SITE_URL."/static/admin/images/test.jpg");
        //实例化图片上传类
        $attr_model = new AdminImage();
        switch ($config_array['oss_type']){
            case 1:
                $rs = $attr_model->aliStrImage($config_array,"test.jpg",$config_array['imgString']);
                if(!empty($rs) && !empty($rs['oss-request-url'])){
                    return 1;
                }
                break;
            case 2:
                $rs = $attr_model->qiniuStrImage($config_array,"test.jpg",$config_array['imgString']);
                if(isset($rs[0]['key']) && !empty($rs[0]['key'])){
                    return 1;
                }
                break;
        }
        return 0;
    }
}